//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using EntLibContrib.Mapping.Properties;

namespace EntLibContrib.Mapping.Configuration.Design
{
	/// <summary>
	/// This class adds commands to the context menus of the nodes in the
	/// Mapping Application Block node hierarchy.
	/// </summary>
	public class MappingCommandRegistrar : CommandRegistrar
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="T:MappingCommandRegistrar"/> class.
		/// </summary>
		/// <param name="serviceProvider">A mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public MappingCommandRegistrar(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Register the commands.
		/// </summary>
		public override void Register ()
		{
			AddMappingSettingsCommand();
			AddGenerateMappersCommand(typeof(MappingSettingsNode));
			AddDefaultCommands(typeof(MappingSettingsNode));

			AddCustomMapperCommand();
			AddCopyCommand(typeof(CustomMapperNode));
			AddDefaultCommands(typeof(CustomMapperNode));

			AddBasicMapperCommand();
			AddCopyCommand(typeof(BasicMapperNode));
			AddDefaultCommands(typeof(BasicMapperNode));

			AddSuperMapperCommand();
			AddCopyCommand(typeof(SuperMapperNode));
			AddDefaultCommands(typeof(SuperMapperNode));

			AddCustomMapCommand();
			AddDefaultCommands(typeof(CustomMapNode));

			AddMapCommand();
			AddDefaultCommands(typeof(MapNode));
		}
		#endregion

		#region Private Methods
		private void AddMappingSettingsCommand ()
		{
			ConfigurationUICommand command = ConfigurationUICommand.CreateSingleUICommand(ServiceProvider,
																																										InternalResources.MappingSettingsUICommandText,
																																										InternalResources.MappingSettingsUICommandLongText,
																																										new AddMappingSettingsNodeCommand(ServiceProvider),
																																										typeof(MappingSettingsNode));
			AddUICommand(command, typeof(ConfigurationApplicationNode));
		}

		private void AddCustomMapperCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.CustomMapperUICommandText,
																	InternalResources.CustomMapperUICommandLongText,
																	typeof(CustomMapperNode),
																	typeof(MappingSettingsNode));
		}

		private void AddBasicMapperCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.BasicMapperUICommandText,
																	InternalResources.BasicMapperUICommandLongText,
																	typeof(BasicMapperNode),
																	typeof(MappingSettingsNode));
		}

		private void AddSuperMapperCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.SuperMapperUICommandText,
																	InternalResources.SuperMapperUICommandLongText,
																	typeof(SuperMapperNode),
																	typeof(MappingSettingsNode));
		}

		private void AddCustomMapCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.CustomMapUICommandText,
																	InternalResources.CustomMapUICommandLongText,
																	typeof(CustomMapNode),
																	typeof(BasicMapperNode));

			AddMultipleChildNodeCommand(InternalResources.CustomMapUICommandText,
																	InternalResources.CustomMapUICommandLongText,
																	typeof(CustomMapNode),
																	typeof(SuperMapperNode));
		}

		private void AddMapCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.MapUICommandText,
																	InternalResources.MapUICommandLongText,
																	typeof(MapNode),
																	typeof(BasicMapperNode));

			AddMultipleChildNodeCommand(InternalResources.MapUICommandText,
																	InternalResources.MapUICommandLongText,
																	typeof(MapNode),
																	typeof(SuperMapperNode));
		}

		private void AddCopyCommand(Type mappingNodeType)
		{
			ConfigurationUICommand command = new ConfigurationUICommand(ServiceProvider,
																																	InternalResources.CopyUICommandText,
																																	InternalResources.CopyUICommandLongText,
																																	CommandState.Enabled,
																																	new CopyMapperNodeCommand(ServiceProvider),
																																	Shortcut.None,
																																	InsertionPoint.Action,
																																	null);
			AddUICommand(command, mappingNodeType);
			command.Dispose();
		}

		private void AddGenerateMappersCommand(Type mappingNodeType)
		{
			ConfigurationUICommand command = new ConfigurationUICommand(ServiceProvider,
																																	InternalResources.GenerateMappersUICommandText,
																																	InternalResources.GenerateMappersUICommandLongText,
																																	CommandState.Enabled,
																																	new GenerateMappersNodeCommand(ServiceProvider),
																																	Shortcut.None,
																																	InsertionPoint.Action,
																																	null);
			AddUICommand(command, mappingNodeType);
			command.Dispose();
		}
		#endregion
	}
}